home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / C Internet Config / IC Application Source ƒ / 68k Internet Config ƒ / C Source ƒ / IC StrH.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-01  |  3.6 KB  |  227 lines  |  [TEXT/SPM ]

  1. /*
  2.     IC StrH.c
  3.     
  4. */
  5.  
  6. #include "IC StrH.h"
  7. #include "IC Misc Subs.h"
  8.  
  9. Handle NewStrH(void){
  10.     return NewHandleClear(sizeof(LineIndex));
  11. }
  12.  
  13. void ReinitStrh(Handle h){
  14.     IndexHandle ih=(IndexHandle)h;
  15.     
  16.     SetHandleSize(h,sizeof(LineIndex));
  17.     **ih=0;
  18. }
  19.  
  20. LineIndex CountStrsH(Handle h){
  21.     IndexHandle ih=(IndexHandle)h;
  22.     
  23.     if (h==(Handle)0)
  24.         return -1;
  25.     return **ih;
  26. }
  27.  
  28. LineIndex CountStrs(short id){
  29.     Handle h=GetResource('STR#',id);
  30.     LineIndex lx=-1;
  31.     
  32.     if (h!=(Handle)0){
  33.         lx=CountStrsH(h);
  34.         ReleaseResource(h);
  35.     }
  36.     
  37.     return lx;
  38. }
  39.  
  40. StringPtr GetIndStr(short strListID,short index,StringPtr str){
  41.     GetIndString(str,strListID,index);
  42.     return str;
  43. }
  44.  
  45. StringPtr GetIndStrH(Handle h,LineIndex index,StringPtr s){
  46.     LineIndex count,i;
  47.     unsigned char* cp;
  48.     SignedByte sb=HGetState(h);
  49.     
  50.     count=CountStrsH(h);
  51.     
  52.     if ((1<=index)&&(index<=count)){
  53.         HLock(h);
  54.         
  55.         cp=(unsigned char*)(*h);
  56.         cp+=sizeof(LineIndex);
  57.         
  58.         for (i=1;i<index;i++){
  59.             cp += ((*cp)+1); // add cur str len
  60.         }
  61.         SetPString(s,1,cp);
  62.         
  63.         HSetState(h,sb);
  64.     } else {
  65.         s[0]=0;
  66.     }
  67.     
  68.     return s;
  69. }
  70.  
  71. void SetIndStrH(Handle h,LineIndex index,StringPtr s){
  72.     LineIndex count,i;
  73.     IndexHandle ih;
  74.     long sz,pos;
  75.     unsigned char* cp;
  76.     SignedByte sb=HGetState(h);
  77.     
  78.     count=CountStrsH(h);
  79.     sz=GetHandleSize(h);
  80.     
  81.     if (count<index){
  82.         SetHandleSize(h,sz+index-count);
  83.         
  84.         HLock(h);
  85.         ih=(IndexHandle)h;
  86.         **ih=index;
  87.         
  88.         cp=(unsigned char*)(*h);
  89.         cp+=sizeof(LineIndex);
  90.         
  91.         for (i=1;i<=count;i++){
  92.             cp+=((*cp)+1); // add cur str len
  93.         }
  94.         for (i=1;i<=index-count;i++){
  95.             *cp=0;
  96.             cp++;
  97.         }
  98.         HSetState(h,sb);
  99.         
  100.         count=index;
  101.     }
  102.     
  103.     if ((index>0)&&(index<=count)){
  104.         HLock(h);
  105.         
  106.         cp=(unsigned char*)(*h);
  107.         cp+=sizeof(LineIndex);
  108.         pos=sizeof(LineIndex);
  109.         
  110.         for (i=1;i<index;i++){
  111.             pos+=((*cp)+1);
  112.             cp+=((*cp)+1); // add cur str len
  113.             
  114.         }
  115.         
  116.         // cp points at the string to replace
  117.         Munger(h,pos,(Ptr)0,(*cp)+1,(Ptr)s,s[0]+1);
  118.         
  119.         HSetState(h,sb);
  120.     }
  121. }
  122.  
  123. void SetIndStr(short id,LineIndex index,StringPtr s){
  124.     Handle h;
  125.     
  126.     h=GetResource('STR#',id);
  127.     HNoPurge(h);
  128.     SetIndStrH(h,index,s);
  129.     HPurge(h);
  130.     ChangedResource(h);
  131.     WriteResource(h);
  132.     ReleaseResource(h);
  133. }
  134.  
  135. void DelIndStrH(Handle h,LineIndex index){
  136.     LineIndex count,i;
  137.     IndexHandle ih;
  138.     long sz,pos;
  139.     unsigned char* cp;
  140.     SignedByte sb=HGetState(h);
  141.     
  142.     count=CountStrsH(h);
  143.     sz=GetHandleSize(h);
  144.     
  145.     if ((index>0)&&(index<=count)){
  146.         HLock(h);
  147.         
  148.         cp=(unsigned char*)(*h);
  149.         cp+=sizeof(LineIndex);
  150.         pos=sizeof(LineIndex);
  151.         
  152.         for (i=1;i<index;i++){
  153.             pos+=((*cp)+1);
  154.             cp+=((*cp)+1); // add cur str len
  155.             
  156.         }
  157.         
  158.         // cp points at the string to replace
  159.         Munger(h,pos,(Ptr)0,(*cp)+1,(Ptr)(&ih),0);
  160.         
  161.         ih=(IndexHandle)h;
  162.         
  163.         (**ih)--;
  164.         
  165.         HSetState(h,sb);
  166.     }
  167. }
  168.  
  169. void DelIndStr(short id,LineIndex index){
  170.     Handle h;
  171.     
  172.     h=GetResource('STR#',id);
  173.     HNoPurge(h);
  174.     DelIndStrH(h,index);
  175.     HPurge(h);
  176.     ChangedResource(h);
  177.     WriteResource(h);
  178.     ReleaseResource(h);
  179. }
  180.  
  181. void InsIndStrH(Handle h,LineIndex index,StringPtr s){
  182.     LineIndex count,i;
  183.     IndexHandle ih;
  184.     long sz,pos;
  185.     unsigned char* cp;
  186.     SignedByte sb=HGetState(h);
  187.     
  188.     count=CountStrsH(h);
  189.     sz=GetHandleSize(h);
  190.     
  191.     if ((index>0)&&(index<=count)){
  192.         HLock(h);
  193.         
  194.         cp=(unsigned char*)(*h);
  195.         cp+=sizeof(LineIndex);
  196.         pos=sizeof(LineIndex);
  197.         
  198.         for (i=1;i<index;i++){
  199.             pos+=((*cp)+1);
  200.             cp+=((*cp)+1); // add cur str len
  201.             
  202.         }
  203.         
  204.         // cp points at the string to replace
  205.         Munger(h,pos,(Ptr)0,0,(Ptr)s,s[0]+1);
  206.         
  207.         ih=(IndexHandle)h;
  208.         
  209.         (**ih)++;
  210.         
  211.         HSetState(h,sb);
  212.     }
  213. }
  214.  
  215. void InsIndString(short id,LineIndex index,StringPtr s){
  216.     Handle h;
  217.     
  218.     h=GetResource('STR#',id);
  219.     HNoPurge(h);
  220.     InsIndStrH(h,index,s);
  221.     HPurge(h);
  222.     
  223.     ChangedResource(h);
  224.     WriteResource(h);
  225.     ReleaseResource(h);
  226. }
  227.